Passing Parameters into a Workflow Instance
Parameters are passed to a workflow instance using the Dictionary object. The CreatWorkflow operation has an overloaded operation that takes not only the workflow type but also a Dictionary object that accepts parameters. The code fragment in this next example demonstrates this:
Example 2.
Type type = typeof(WFWorkflow.CalcWorkflow); Dictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add("Value1", 11); parameters.Add("Value2", 19); parameters.Add("Operation", "+"); WorkflowInstance instance = workflowRuntime.CreateWorkflow(type, parameters); instance.Start();
|
Returning Parameters from a Workflow Instance
The WorkflowCompletedEventArgs in the WorkflowCompleted event returns an output parameter, as shown here:
Example 3.
AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate (object sender, WorkflowCompletedEventArgs e) { int total = (int)e.OutputParameters["Result"]; Console.WriteLine(total); waitHandle.Set(); };
|
Output parameters are
collections and data is extracted from collection and cast into an
integer. The names of the input parameters passed into the workflow
instance automatically map to the names of properties defined inside the
workflow class. In this case, the Result property maps directly to the result parameter returned. The input properties are used to initialize private variables in the workflow instance:
Example 4.
public partial class CalcWorkflow : SequentialWorkflowActivity { private int value1 = 0; private int value2 = 0; private int result = 0; private String operation;
public int Value1 { set{value1 = value;} } public int Value2 { set{value2 = value;} } public string Operation { set{operation = value;} } public int Result { get{return result;} } private void Calculator_ExecuteCode (object sender, EventArgs e) { if (operation == "+") {result = value1 + value2;} } }
|